package com.squirrel.common.controller;

import com.squirrel.common.util.FileUploadUtils;
import com.squirrel.common.param.user.LoginParam;
import com.squirrel.common.exception.BaseException;
import com.squirrel.common.param.user.PhoneParam;
import com.squirrel.common.response.ResponseResult;
import com.squirrel.common.response.ResponseResultUtils;
import com.squirrel.common.response.code.CodeEnum;
import com.squirrel.common.util.ImageToolUtils;
import com.squirrel.common.param.video.MultipartFileParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * @Description 控制器-测试
 * @Author 关远键
 * @Version v1.0.0
 * @Date 2021/11/7 16:23
 */
@RestController
@RequestMapping("/api/test")
@Api(tags = "测试控制器")
public class TestController {

    @PostMapping("/getCode")
    @ApiOperation("手机验证码")
    public String getCode(PhoneParam phoneParam) {
        return "都说要钱的咯，你还点，钱啊！钱！！！";
    }

    @GetMapping("/login")
    @ApiOperation("测试shiro")
    public String login(LoginParam loginParam, Model model) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginParam.getMailOrPhone(), loginParam.getPassword());
        try {
//            subject.login(token);
//            return "Login Success.";
            return "请登录。";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg", "用户名不存在");
            throw new BaseException(CodeEnum.NOT_EXISTS);
        } catch (IncorrectCredentialsException e) {
            throw new BaseException(CodeEnum.LOGIN_FAIL);
        }

    }

    @GetMapping("/unauthorized")
    @ApiOperation("无权限跳转")
    public String unauthorized() {
        return "请提升权限后访问。";
    }

    @GetMapping("/exception")
    public ResponseResult exception() {
        return ResponseResultUtils.error(CodeEnum.INVALID_RESOURCE);
    }

    @Value("${file.upload.path}")
    private String fileSavePath;

    @Value("${file.coming.path}")
    private String fileComingPath;

    @ApiOperation("大文件分片上传")
    @PostMapping(value = "/chunkUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public String fileChunkUpload(MultipartFileParam param) throws Exception {
        String root = fileSavePath;
        //验证文件夹规则,不能包含特殊字符
        File file = new File(root);
        String path = file.getAbsolutePath();
        return FileUploadUtils.chunkUploadByMappedByteBuffer(param, root);

    }

    @ApiOperation("截取图片")
    @PostMapping(value = "/randomGrabberFFmpegVideoImage", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public String randomGrabberFFmpegVideoImage(String path) throws Exception {
        return ImageToolUtils.randomGrabberFFmpegVideoImage(path);
    }

}
